package com.lxl.xm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lxl.xm.service.AdvertiserService;
import com.lxl.xm.model.entity.Advertiser;
import com.lxl.xm.mapper.AdvertiserMapper;

import com.lxl.xm.model.entity.Advertiser;
import com.lxl.xm.service.AdvertiserService;
import com.lxl.xm.mapper.AdvertiserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.print.Pageable;
import java.util.List;
import java.util.Random;

/**
* @author 86135
* @description 针对表【advertiser(广告商家表)】的数据库操作Service实现
* @createDate 2024-02-06 15:39:33
*/
@Service
public class AdvertiserServiceImpl extends ServiceImpl<AdvertiserMapper, Advertiser>
    implements AdvertiserService {
    @Resource
    private AdvertiserMapper advertiserMapper;
    @Resource
    JavaMailSender mailSender;

    @Override
    public Page<Advertiser> getAdvertiserPage(int current, int size) {
        return advertiserMapper.selectAdvertiserPage(new Page<>(current,size));
    }

    @Override
    public boolean createAdvertiser(Advertiser advertiser) {
        return this.save(advertiser);
    }

    @Override
    public boolean deleteAdvertiserById(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean updateAdvertiser(Advertiser advertiser) {
        return this.updateById(advertiser);
    }

    @Override
    public Advertiser getAdvertiserById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<Advertiser> getPendingAdvertisers() {
        QueryWrapper<Advertiser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("approval_status",0);//0 代表未审核
        return advertiserMapper.selectList(queryWrapper);
    }

    @Override
    public void reviewAdvertiser(Long id, Integer status, String notes) {
        Advertiser advertiser = new Advertiser();
        advertiser.setId(id);
        advertiser.setApprovalStatus(status);
        advertiser.setNotes(notes);
        int result = advertiserMapper.updateById(advertiser);
        if(result!=1){
            throw new RuntimeException("审核更新失败");
        }
    }

    @Override
    public void suspendAdvertiser(Long id) {
        Advertiser advertiser = new Advertiser();
        advertiser.setId(id);
        advertiser.setIsDeleted(1);
        advertiserMapper.updateById(advertiser);
    }

    @Override
    public Page<Advertiser> searchAdvertisers(String keyword, int page ,int size) {
        QueryWrapper<Advertiser>queryWrapper = new QueryWrapper<>();
        queryWrapper.like("advertiser_name",keyword)
                .or()
                .like("email",keyword)
                .or()
                .like("contact_info",keyword);
        int current = (page-1)*size;
        return advertiserMapper.selectPage(new Page<>(current,size),queryWrapper);
    }

    @Override
    public Integer getCount() {
        return this.baseMapper.getcount();
    }


/*
    @Override
    public boolean sendVerificationCode(String email) {
        String code = generateVerificationCode(String email);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1735162844@qq.com");
        message.setTo(email);
        message.setSubject("注册验证码");
        message.setText("您的注册验证码是："+code);
        mailSender.send(message);
        saveVerificationCode(email,code);
        return true;
    }

    @Override
    public boolean registerAdvertiser(Advertiser advertiser, String code) {
        if(!validateVerificationCode(advertiser.getEmail(),code)){
            return false;
        }
        //加密密码
        advertiser.setAdvertiserPassword(encryptPassword(advertiser.getAdvertiserPassword()));
        advertiserMapper.insert(advertiser);
        return true;
    }

    @Override
    public Advertiser loginAdvertiser(String email, String password) {
        Advertiser advertiser = advertiserMapper.selectByEmail(email);
        if(advertiser!=null&&checkPassword(password,advertiser.getAdvertiserPassword())){
            return advertiser;
        }
        return null;
    }
    private String generateVerificationCode(){
        return String.valueOf(new Random().nextInt(899999)+1000000);
    }
    private boolean saveVerificationCode(String email,String code){
        //保存验证码到数据库或缓存中
        return true;
    }
    private boolean validateVerificationCode(String eamil,String code){
        //获取验证码并校验
        return true;
    }
    private String encryptPassword(String password){
        //加密算法
        return password;
    }
    private boolean checkPassword(String inputPassword,String storedPassword){
        //校验输入密码与加密密码是否匹配
        return inputPassword.equals(storedPassword);
    }

 */
}




